package com.dorado.example.service;

import com.dorado.example.pojo.*;
import com.dorado.kernel.excel.ImportLifeHandler;
import com.dorado.kernel.excel.annotation.ExportFile;
import com.dorado.kernel.excel.annotation.ImportFile;
import com.dorado.kernel.excel.enums.Destination;
import com.dorado.kernel.excel.enums.ImportSource;
import com.dorado.kernel.excel.enums.TaskType;
import com.dorado.kernel.excel.model.ImportError;
import com.dorado.kernel.excel.model.ImportResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class ExcelService {

	public static class TestListHandler implements ImportLifeHandler {
		@Override
		public void onField(int x, int y, Object field, Object row, List<Object> list) throws Exception {
			log.info("生命周期接口-校验字段");
		}

		@Override
		public void onRow(int y, Object row, Object list) throws Exception {
			log.info("生命周期接口-校验实体");
		}

		@Override
		public void onTable(List<Object> list) throws Exception {
			log.info("生命周期接口: 校验完整数据");
			throw new Exception("股数总数超过限额!");
		}
		
	}
	
	@ImportFile(source = ImportSource.InputStream, targetClass = EmployeeRecord.class, failfastRows = 1, lifeHandler = TestListHandler.class)
	public void importEsop(InputStream input, String fileName, ImportResult result) {
		if(result.getHasErrors()){
			List<ImportError> errors = result.getErrors();
			log.info("导入错误:");
			for(ImportError error : errors) {
				log.info("位置[" + error.getX() + ", " + error.getY() + "]消息[" + error.getMessage() + "]");
			}
		}else {
			log.info("导入成功~[" + result.getData().size() + "]");
			List<EmployeeRecord> data = (List<EmployeeRecord>)result.getData();
			for(EmployeeRecord record: data) {
				log.info("数据:" + record);
			}
		}
		log.info("处理数据条数~[" + result.getData().size() + "]");
		log.info("失败数据条数~[" + result.getFailedRowCount() + "]");
	}

	@ExportFile(sheetName = "Demo", title = "订单信息标题", destination = Destination.OutputStream, taskType = TaskType.NORMAL)
	public List<User> exportUserSync(OutputStream output, String owerId){
		
		return getUserList();
	}


	@ExportFile(sheetName = "Demo", title = "门店信息标题", destination = Destination.OutputStream, taskType = TaskType.NORMAL)
	public List<Shop> exportShopSync(OutputStream output, String owerId){

		return getShopList();
	}

	@ExportFile(destination = Destination.OutputStream)
	public List<User> exportUserAsync(String owerId){
		
		return getUserList();
	}


	public String getName(){
		String[] names = {"哎", "雪","恒","永","制","楠","卟","の","斯","欲"};
		StringBuffer stringBuffer = new StringBuffer();
		int size = 2 + new Random().nextInt(2);
		for (int i = 0; i < size; i++) {
			//(int)(Math.random()*10+1)
			int n = new Random().nextInt(10);
			stringBuffer.append(names[n]);
		}
		return stringBuffer.toString();
	}


	public  List<Shop> getShopList(){
		List<Shop> shops = new ArrayList<>();

		for (int i = 0; i < 10; i++) {
			//System.out.println(getName());
			int income = 9000 + new Random().nextInt(500);
			Shop shop = new Shop(getName(),income);
			shop.setShopCode("SX0000000001");
			shop.setCreateDate(new Date());
			shop.setShopType(1+new Random().nextInt(3));
			Manager manager = new Manager("掌柜"+i,"女","12年");
			shop.setManager(manager);
			int size = 6;//3+ new Random().nextInt(3);
			List<Clerk> clerks = new ArrayList<>();
			for (int j = 0; j < size; j++) {
				int clerkInCome = (9000 + new Random().nextInt(500))/size;
				if (income - clerkInCome < 0) {
					clerkInCome = income;
				}
				Clerk clerk = new Clerk(getName(),18+new Random().nextInt(7)+"",clerkInCome);
				clerks.add(clerk);
			}
			shop.setClerks(clerks);
			shops.add(shop);
		}
		return shops;
	}
	private List<User> getUserList(){
		List<User> users = new ArrayList<>();
		User a = new User();
		a.setName("小王");
		a.setBirthday(new Date());
		a.setAge(20);
		a.setDeposit(new BigDecimal("100000"));
		a.setEmail("xiaowang@qq.com");
		a.setMobile("18888888");
		a.setAddress("我们使用autoSizeColumn方法可以把Excel设置为根据内容自动调整列宽，然而这个方法对中文并不起效，只对数字和字母有效");
		users.add(a);
		for(int i = 0; i < 9999; i++) {
			User b = new User();
			BeanUtils.copyProperties(a, b);
			users.add(b);
		}
		
		return users;
	}
}
